<?php

class User_model extends CI_Model {
	public function __construct()
	{
		 parent::__construct();
		 
	}
	function validate()
	{
		$this->db->where('email', $this->input->post('email'));
		$this->db->where('password', md5($this->input->post('password')));
		$query = $this->db->get('users');
		
		if($query->num_rows == 1)
		{
			$loggedin=array("logged_in"=>1);
			$update_logged_in=$this->general_model->update('users',$loggedin,'email = '."'".$this->input->post('email')."'".' and password = '."'".md5($this->input->post('password'))."'");
			$userdetails=$this->general_model->select_val('users','*','activated = 1 and email = '."'".$this->input->post('email')."'".' and password = '."'".md5($this->input->post('password'))."'");
			
					
			$this->session->set_userdata($loggedin);
			$this->session->set_userdata("user",$userdetails);
		
			return true;
		}
		
	}
	function loggedin()
	{
		return (bool) $this->session->userdata('logged_in');
	}
	function logout ()
	{
		$this->session->sess_destroy();
	}
	function create_member()
	{
		
		$new_member_insert_data = array(
			'first_name' => $this->input->post('first_name'),
			'last_name' => $this->input->post('last_name'),
			'email_address' => $this->input->post('email_address'),			
			'username' => $this->input->post('username'),
			'password' => md5($this->input->post('password'))						
		);
		
		$insert = $this->db->insert('membership', $new_member_insert_data);
		return $insert;
	}
}